home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / graphics.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  7.9 KB  |  320 lines

  1. /*
  2.  * @(#)GraphicsTest.java    1.3 95/09/01 Sami Shaio
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. import java.awt.*;
  20. import java.applet.*;
  21.  
  22. public class GraphicsTest extends Applet {
  23.     GraphicsCards    cards;
  24.     public void init() {
  25.     setLayout(new BorderLayout());
  26.     add("Center", cards = new GraphicsCards());
  27.     Panel p = new Panel();
  28.     p.add(new Button("next"));
  29.     p.add(new Button("previous"));
  30.     p.add(new Label("go to:", Label.RIGHT));
  31.  
  32.     Choice c;
  33.  
  34.     p.add(c = new Choice());
  35.     c.addItem("Arc");
  36.     c.addItem("Oval");
  37.     c.addItem("Polygon");
  38.     c.addItem("Rect");
  39.     c.addItem("RoundRect");
  40.     add("North", p);
  41.     }
  42.     public boolean action(Event evt, Object arg) {
  43.     if (evt.target instanceof Choice) {
  44.         ((CardLayout)cards.getLayout()).show(cards,(String)arg);
  45.     } else {
  46.         if ("next".equals(arg)) {
  47.         ((CardLayout)cards.getLayout()).next(cards);
  48.         } else if ("previous".equals(arg)) {
  49.         ((CardLayout)cards.getLayout()).previous(cards);
  50.         }
  51.     }
  52.  
  53.     return true;
  54.     }
  55.  
  56.     public static void main(String args[]) {
  57.     GraphicsFrame f = new GraphicsFrame();
  58.     GraphicsTest graphicsTest = new GraphicsTest();
  59.  
  60.     graphicsTest.init();
  61.     f.add("Center", graphicsTest);
  62.     f.resize(300, 300);
  63.     f.show();
  64.     }
  65. }
  66.  
  67. class GraphicsFrame extends Frame {
  68.     public GraphicsFrame() {
  69.     super("Graphics Test");
  70.     }
  71.     public boolean handleEvent(Event e) {
  72.     if (e.id == Event.WINDOW_DESTROY) {
  73.         System.exit(0);
  74.     }
  75.     return false;
  76.     }
  77. }
  78.     
  79. class GraphicsCards extends Panel {
  80.     public GraphicsCards() {
  81.     setLayout(new CardLayout());
  82.     add("Arc", new ArcCard());
  83.     add("Oval", new ShapeTest(new OvalShape()));
  84.     add("Polygon", new ShapeTest(new PolygonShape()));
  85.     add("Rect", new ShapeTest(new RectShape()));
  86.     add("RoundRect", new ShapeTest(new RoundRectShape()));
  87.     }
  88. }
  89.  
  90.  
  91. class ArcCard extends Panel {
  92.     public ArcCard() {
  93.     setLayout(new GridLayout(0, 2));
  94.     add(new ArcPanel(true));
  95.     add(new ArcPanel(false));
  96.     add(new ArcDegreePanel(true));
  97.     add(new ArcDegreePanel(false));
  98.     }
  99. }
  100.  
  101. class ArcDegreePanel extends Panel {
  102.     boolean filled;
  103.     public ArcDegreePanel(boolean filled) {
  104.     this.filled = filled;
  105.     }
  106.     void arcSteps(Graphics g,
  107.           int step, int x, int y, int w, int h,
  108.           Color c1,
  109.           Color c2) {
  110.     int a1 = 0;
  111.     int a2 = step;
  112.     int progress = 0;
  113.     g.setColor(c1);
  114.     for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1 ) {
  115.         if (g.getColor() == c1) {
  116.         g.setColor(c2);
  117.         } else {
  118.         g.setColor(c1);
  119.         }
  120.         if (filled) {
  121.         g.fillArc(x, y, w, h, a1, a2);
  122.         } else {
  123.         g.drawArc(x, y, w, h, a1, a2);
  124.         }
  125.         progress = a1+a2;
  126.     }
  127.     if (progress != 360) {
  128.         if (filled) {
  129.         g.fillArc(x, y, w, h, a1, 360 - progress);
  130.         } else {
  131.         g.drawArc(x, y, w, h, a1, 360 - progress);
  132.         }
  133.     }
  134.     }
  135.     public void paint(Graphics g) {
  136.     Rectangle r = bounds();
  137.  
  138.     arcSteps(g, 3, 0, 0, r.width, r.height,
  139.          Color.orange, Color.blue);
  140.     arcSteps(g, 2, r.width / 4, r.height / 4, r.width / 2, r.height / 2,
  141.          Color.yellow, Color.green);
  142.     arcSteps(g, 1,
  143.          (r.width  * 3) / 8,
  144.          (r.height * 3) / 8,
  145.          r.width / 4, r.height / 4,
  146.          Color.magenta, Color.white);
  147.     }
  148. }
  149.  
  150. class ArcPanel extends Panel {
  151.     boolean filled;
  152.     public ArcPanel(boolean filled) {
  153.     this.filled = filled;
  154.     }
  155.     public void paint(Graphics g) {
  156.     Rectangle r = bounds();
  157.     g.setColor(Color.yellow);
  158.     if (filled) {
  159.         g.fillArc(0, 0, r.width, r.height, 0, 45);
  160.     } else {
  161.         g.drawArc(0, 0, r.width, r.height, 0, 45);
  162.     }
  163.     g.setColor(Color.green);
  164.     if (filled) {
  165.         g.fillArc(0, 0, r.width, r.height, 90, -45);
  166.     } else {
  167.         g.drawArc(0, 0, r.width, r.height, 90, -45);
  168.     }
  169.     g.setColor(Color.orange);
  170.     if (filled) {
  171.         g.fillArc(0, 0, r.width, r.height, 135, -45);
  172.     } else {
  173.         g.drawArc(0, 0, r.width, r.height, 135, -45);
  174.     }
  175.     g.setColor(Color.magenta);
  176.     if (filled) {
  177.         g.fillArc(0, 0, r.width, r.height, -225, 45);
  178.     } else {
  179.         g.drawArc(0, 0, r.width, r.height, -225, 45);
  180.     }
  181.  
  182.     g.setColor(Color.yellow);
  183.     if (filled) {
  184.         g.fillArc(0, 0, r.width, r.height, 225, -45);
  185.     } else {
  186.         g.drawArc(0, 0, r.width, r.height, 225, -45);
  187.     }
  188.     g.setColor(Color.green);
  189.     if (filled) {
  190.         g.fillArc(0, 0, r.width, r.height, -135, 45);
  191.     } else {
  192.         g.drawArc(0, 0, r.width, r.height, -135, 45);
  193.     }
  194.     g.setColor(Color.orange);
  195.     if (filled) {
  196.         g.fillArc(0, 0, r.width, r.height, -45, -45);
  197.     } else {
  198.         g.drawArc(0, 0, r.width, r.height, -45, -45);
  199.     }
  200.     g.setColor(Color.magenta);
  201.     if (filled) {
  202.         g.fillArc(0, 0, r.width, r.height, 315, 45);
  203.     } else {
  204.         g.drawArc(0, 0, r.width, r.height, 315, 45);
  205.     }
  206.     }
  207. }
  208.  
  209. abstract class Shape {
  210.     abstract void draw(Graphics g, int x, int y, int w, int h);
  211.     abstract void fill(Graphics g, int x, int y, int w, int h);
  212. }
  213.  
  214. class RectShape extends Shape {
  215.     void draw(Graphics g, int x, int y, int w, int h) {
  216.     g.drawRect(x, y, w, h);
  217.     }
  218.     void fill(Graphics g, int x, int y, int w, int h) {
  219.     g.fillRect(x, y, w, h);
  220.     }
  221. }
  222.  
  223. class OvalShape extends Shape {
  224.     void draw(Graphics g, int x, int y, int w, int h) {
  225.     g.drawOval(x, y, w, h);
  226.     }
  227.     void fill(Graphics g, int x, int y, int w, int h) {
  228.     g.fillOval(x, y, w, h);
  229.     }
  230. }
  231.  
  232. class RoundRectShape extends Shape {
  233.     void draw(Graphics g, int x, int y, int w, int h) {
  234.     g.drawRoundRect(x, y, w, h, 10, 10);
  235.     }
  236.     void fill(Graphics g, int x, int y, int w, int h) {
  237.     g.fillRoundRect(x, y, w, h, 10, 10);
  238.     }
  239. }
  240.  
  241. class PolygonShape extends Shape {
  242.     Polygon p;
  243.     public PolygonShape() {
  244.     p = new Polygon();
  245.     p.addPoint(0, 0);
  246.     p.addPoint(10, 0);
  247.     p.addPoint(5, 15);
  248.     p.addPoint(10, 20);
  249.     p.addPoint(5, 20);
  250.     p.addPoint(0, 10);
  251.     p.addPoint(0, 0);
  252.     }
  253.     void draw(Graphics g, int x, int y, int w, int h) {
  254.     Graphics ng = g.create();
  255.     ng.translate(x, y);
  256.     ng.scale((float)((float)w / (float)10), (float)((float)h / (float)20));
  257.     ng.drawPolygon(p);
  258.     }
  259.     void fill(Graphics g, int x, int y, int w, int h) {
  260.     Graphics ng = g.create();
  261.     ng.translate(x, y);
  262.     ng.scale((float)((float)w / (float)10), (float)((float)h / (float)20));
  263.     ng.fillPolygon(p);
  264.     }
  265. }
  266.  
  267. class ShapeTest extends Panel {
  268.     Shape    shape;
  269.     int        step;
  270.  
  271.     public ShapeTest(Shape shape, int step) {
  272.     this.shape = shape;
  273.     this.step = step;
  274.     }
  275.  
  276.     public ShapeTest(Shape shape) {
  277.     this(shape, 10);
  278.     }
  279.  
  280.     public void paint(Graphics g) {
  281.     Rectangle bounds = bounds();
  282.  
  283.     int cx, cy, cw, ch;
  284.  
  285.     Color color;
  286.     for (color=Color.red, cx=bounds.x, cy=bounds.y,
  287.          cw=bounds.width / 2, ch=bounds.height; 
  288.          cw > 0 && ch > 0;
  289.          cx+=step, cy += step, cw -= (step * 2), ch -= (step * 2),
  290.          color=ColorUtils.darker(color, 0.9)) {
  291.         g.setColor(color);
  292.         shape.draw(g, cx, cy, cw, ch);
  293.     }
  294.     for (cx=bounds.x + bounds.width / 2, cy=bounds.y,
  295.          cw=bounds.width / 2, ch=bounds.height; 
  296.          cw > 0 && ch > 0;
  297.          cx+=step, cy += step, cw -= (step * 2),ch -= (step * 2)) {
  298.         if (g.getColor() == Color.red) {
  299.         g.setColor(Color.blue);
  300.         } else {
  301.         g.setColor(Color.red);
  302.         }
  303.         shape.fill(g, cx, cy, cw, ch);
  304.     }
  305.     }
  306. }
  307.  
  308. class ColorUtils {
  309.     static Color brighter(Color c, double factor) {
  310.     return new Color(Math.min((int)(c.getRed()  *(1/factor)), 255), 
  311.              Math.min((int)(c.getGreen()*(1/factor)), 255),
  312.              Math.min((int)(c.getBlue() *(1/factor)), 255));
  313.     }
  314.     static Color darker(Color c, double factor) {
  315.     return new Color(Math.max((int)(c.getRed()  *factor), 0), 
  316.              Math.max((int)(c.getGreen()*factor), 0),
  317.              Math.max((int)(c.getBlue() *factor), 0));
  318.     }
  319. }
  320.